package exam.huatai.S1;

import java.util.*;

public class Main {

    public static boolean flag;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int caseNum = 1;
        while (true) {
            int n = 0;
            List<Integer> start = new ArrayList<>();
            List<Integer> end = new ArrayList<>();
            Map<Integer, Integer> map = new HashMap<>();
            boolean hasNext = true;
            while (true) {
                int u = sc.nextInt();
                int v = sc.nextInt();
                if (u == 0 && v == 0)
                    break;
                if (u == -1 && v == -1) {
                    hasNext = false;
                    break;
                }
                if (!map.containsKey(u))
                    map.put(u, n++);
                if (!map.containsKey(v))
                    map.put(v, n++);
                start.add(u);
                end.add(v);
            }
            if (!hasNext)
                break;
            if (n == 0)
                System.out.printf("Case %d is a tree.\n", caseNum);
            else {
                List<Integer>[] graph = new List[n];
                int[] pre = new int[n];
                for (int i = 0; i < n; i++)
                    graph[i] = new ArrayList<>();
                for (int i = 0; i < start.size(); i++) {
                    int u = map.get(start.get(i));
                    int v = map.get(end.get(i));
                    graph[u].add(v);
                    pre[v]++;
                }
                if (solution(graph, pre, n))
                    System.out.printf("Case %d is a tree.\n", caseNum);
                else
                    System.out.printf("Case %d is not a tree.\n", caseNum);
            }
            caseNum++;
        }
    }

    private static boolean solution(List<Integer>[] graph, int[] pre, int n) {
        if (n == 0)
            return true;
        int root = -1, num = 0;
        for (int i=0; i<pre.length; ++i) {
            if (pre[i] == 0) {
                root = i;
                num++;
            }
        }
        if (num != 1)
            return false;
        boolean[] visited = new boolean[n];
        visited[root] = true;
        flag = true;
        dfs(graph, visited, root);
        if (!flag)
            return false;
        for (int i=0; i<visited.length; ++i) {
            if (!visited[i])
                return false;
        }
        return true;
    }

    private static void dfs(List<Integer>[] graph, boolean[] visited, int root) {
        if (!flag)
            return;
        for (int i=0; i<graph[root].size(); ++i) {
            int v = graph[root].get(i);
            if (visited[v]) {
                flag = false;
                return;
            }
            visited[v] = true;
            dfs(graph, visited, v);
        }
    }
}
